import {defineStore} from "pinia";
import {usePmProjectApi} from "@/api/pm/project/project";

type PmProjectState = {
  projectNumber: string | null
  projectStatus: ProjBusinessProcessResult,
  projectBasicInfo: ProjectDetail,
}

export const usePmProjectStore = defineStore('PmProject', {
  state: (): PmProjectState => ({
    projectNumber: null,
    projectStatus: null,
    projectBasicInfo: null,
  }),
  actions: {
    async getProject(projectNumber: string) {
      const projDetailRes = await usePmProjectApi().getProjectDetailInfo(projectNumber)
      if (projDetailRes.code === 0) {
        this.projectBasicInfo = projDetailRes.data
        const statusData = await usePmProjectApi().getProjectProcessStatus(projectNumber)
        this.projectStatus = statusData.data
      }
    },
    async setProjectNumber(projectNumber: string | null) {
      if (projectNumber) {
        await this.getProject(projectNumber);
      } else {
        this.projectNumber = null;
        this.projectStatus = null;
      }
    }
  },
  persist: true,
})
